package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.otherActivity.VideoPlayerActivity;
import com.avic.rong.course.bean.PeiXunClassMuLuBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.utils.AppUtils;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/18 10:31
 * E-Mail Address：16601203965@163.com
 */
public class CourseMuLuSecondAdapter extends RecyclerView.Adapter<CourseMuLuSecondAdapter.CourseMuLuSecondViewHolder> {

    private Context context;
    private List<PeiXunClassMuLuBean.DatasBean.HourListBean> datasBeanList;
    private LayoutInflater inflater;
    private String firstImageUrl = ConstantsUtils.EMPTY_STR;

    public CourseMuLuSecondAdapter(Context context, List<PeiXunClassMuLuBean.DatasBean.HourListBean>  datasBeanList,String firstImageUrl) {
        this.context = context;
        this.datasBeanList = datasBeanList;
        this.firstImageUrl = firstImageUrl;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public CourseMuLuSecondViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_course_mulu_item_second, viewGroup, false);
        return new CourseMuLuSecondViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseMuLuSecondViewHolder holder, int position) {

        PeiXunClassMuLuBean.DatasBean.HourListBean hourListBean = datasBeanList.get(position);
        Logger.i("------------------dd----" + AppUtils.COURSE_IMAGE_URL + firstImageUrl);
        Glide.with(context).load(AppUtils.COURSE_IMAGE_URL + firstImageUrl).placeholder(R.mipmap.online_default).into(holder.mulu_img);
        holder.mulu_title.setText(hourListBean.getName());
        holder.mulu_time.setText("时长：" + hourListBean.getTotalTime());
        holder.itemView.setOnClickListener(new MuLuListener(hourListBean,firstImageUrl));


    }

    @Override
    public int getItemCount() {
        return datasBeanList.size();
    }


    class MuLuListener implements View.OnClickListener{
        PeiXunClassMuLuBean.DatasBean.HourListBean hourListBean;
        String firstImageUrl;

        public MuLuListener(PeiXunClassMuLuBean.DatasBean.HourListBean hourListBean,String firstImageUrl ) {
            this.hourListBean = hourListBean;
            this.firstImageUrl = firstImageUrl;
        }

        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(hourListBean.getContent())){
                ToastTools.showPublicToast("您没有权限，请购买权限");
                return;
            }
            context.startActivity(VideoPlayerActivity.getIntent(context, hourListBean.getName(),hourListBean.getContent(),firstImageUrl));
        }
    }

    class CourseMuLuSecondViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.mulu_img)
        ImageView mulu_img;
        @BindView(R.id.mulu_start)
        ImageView mulu_start;
        @BindView(R.id.mulu_title)
        TextView mulu_title;
        @BindView(R.id.mulu_time)
        TextView mulu_time;

        public CourseMuLuSecondViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
